home *** CD-ROM | disk | FTP | other *** search
- Path: waldorf.csc.calpoly.edu!galaxy!ostoll
- From: ostoll@galaxy.csc.calpoly.edu (Oliver Stoll)
- Newsgroups: comp.lang.c++
- Subject: Re: Copy constructor optimization..
- Date: 5 Mar 1996 22:24:35 GMT
- Organization: Cal Poly Computer Science Dept.
- Message-ID: <4hiev3$5dd@waldorf.csc.calpoly.edu>
- References: <9602291637.aa17139@paris.ics.uci.edu>
- NNTP-Posting-User: ostoll@galaxy.csc.calpoly.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Michael Heiberg (mheiberg@ahi.ICS.UCI.EDU) wrote:
- : When constructing a variable from the return value of a function that
- : returns a copy of that same type, the copy constructor will be called,
- : but should the return statement copy the answer into the return space,
- : and then copy that into the object being constructed, or should the
- : return value be copy-constructed directly into the being-constructed
- : object? For example:
-
- : #include <iostream.h>
-
- : class Vector {
- : public:
- : Vector() { cout << "Ctor "; }
- : Vector(Vector& v) { cout << "Copy "; }
- : };
-
- : Vector foo(Vector &v) { return v; } // calls the copy-ctor
-
- : main() {
- : Vector a; // ctor
- : Vector b=foo(a); // copy-ctor
- : }
-
- : Output:
- : Ctor Copy
-
- : Function foo returns a Vector by copy, calling the copy constructor, but
- : Vector b needs to be constructed with that return value. I have tried
- : this out on a Gnu, Borland, and Microsoft compiler, with optimizations
- : turned off, and they all do this operation in one step, producing the
- : output listed above. Is this just a very common and trivial optimization,
- : or does the language actually specify that the constructed object is
- : copy-constructed with v of foo?
-
- : Any thoughts, answers, or references to relevant sections of one of
- : Stroustrup's books would be appreciated. Reply by email please.
-
- : Michael Heiberg - mheiberg@ics.uci.edu - http://www.ics.uci.edu/~mheiberg
-
-
- If you return an object by value, then you return a copy of the object, and
- thus get a copy constructor call to create that copy.
-
-
- Oliver
-
- --
- -------------------------------------------------------------------------------
- Oliver "Oyl McDoyle" Stoll
-
- Fachhochschule Karlsruhe California Polytechnic State
- stol0012@fh-karlsruhe.de University (San Luis Obispo)
- http://www.fh-karlsruhe.de/~stol0012 ostoll@galaxy.csc.calpoly.edu
-